package com.my_musc_service.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my_musc_service.dao.SharedSongListDao;
import com.my_musc_service.entity.SharedSongList;
import com.my_musc_service.entity.SharedSongListVO;
import com.my_musc_service.exception.CustomException;
import com.my_musc_service.service.SharedSongListService;
import com.my_musc_service.service.SongListCommonProp;
import com.my_musc_service.utils.JudgeFlagUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * (SharedSongList)表服务实现类
 *
 * @author A.Dun
 * @since 2021-08-17 14:14:38
 */
@Service("sharedSongListService")
public class SharedSongListServiceImpl extends ServiceImpl<SharedSongListDao, SharedSongList> implements SharedSongListService, SongListCommonProp {
    @Resource
    private SharedSongListDao sharedSongListDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public SharedSongList queryById(Integer id) {
        return this.sharedSongListDao.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<SharedSongList> queryAllByLimit(int offset, int limit) {
        return this.sharedSongListDao.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param sharedSongList 实例对象
     * @return 实例对象
     */
    @Override
    public Boolean insert(SharedSongList sharedSongList) {
        int count = this.sharedSongListDao.insert(sharedSongList);
        return JudgeFlagUtils.isChanged(count);
    }

    /**
     * 修改数据
     *
     * @param sharedSongList 实例对象
     * @return 实例对象
     */
    @Override
    public SharedSongList update(SharedSongList sharedSongList) {
        this.sharedSongListDao.update(sharedSongList);
        return this.queryById(sharedSongList.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer id) {
        return this.sharedSongListDao.deleteById(id) > 0;
    }

    @Override
    public Boolean addClickCount(SharedSongList sharedSongList) {
        List<SharedSongList> sharedSongs = baseMapper.queryAll(sharedSongList);
        if (!CollectionUtils.isEmpty(sharedSongs)) {
            SharedSongList sharedSongListData = sharedSongs.get(0);
            Integer clickCount = sharedSongListData.getClickCount();
            if (clickCount != null) {
                clickCount += 1;
            }else {
                clickCount=0;
                clickCount += 1;
            }
            sharedSongListData.setClickCount(clickCount);
            int count = baseMapper.update(sharedSongListData);
            return JudgeFlagUtils.isChanged(count);
        } else {
            throw new CustomException("更新失败");
        }
    }

    @Override
    public Boolean delClickCount(SharedSongList sharedSongList) {
        List<SharedSongList> sharedSongs = baseMapper.queryAll(sharedSongList);
        if (!CollectionUtils.isEmpty(sharedSongs)) {
            SharedSongList sharedSongListData = sharedSongs.get(0);
            Integer clickCount = sharedSongListData.getClickCount();
            if (clickCount != null && clickCount > 0) {
                clickCount -= 1;
            }
            sharedSongListData.setClickCount(clickCount);
            int count = baseMapper.update(sharedSongListData);
            return JudgeFlagUtils.isChanged(count);
        } else {
            throw new CustomException("更新失败");
        }
    }


    @Override
    public Map<String, Object> getSharedSongListData(Page<SharedSongListVO> sharedSongListPageParam) {
        IPage<SharedSongListVO> sharedSongListVOIPage = selectPage(sharedSongListPageParam, null);
        long total = sharedSongListVOIPage.getTotal();
        //每一个分享歌单封面是其第一首歌的封面
        List<SharedSongListVO> sharedSongListVOList = sharedSongListVOIPage.getRecords();
        setSongListCover(sharedSongListVOList);
//        for (SharedSongListVO sharedSongListVO : sharedSongListVOList) {
//            Integer songListId = sharedSongListVO.getSongListId();
//            Integer userId = sharedSongListVO.getUserId();
//            QueryWrapper<SongListUser> songListUserQueryWrapper = new QueryWrapper<>();
//            songListUserQueryWrapper.eq("song_list_id", songListId);
//            songListUserQueryWrapper.eq("user_id", userId);
//            songListUserQueryWrapper.orderByAsc("song_index");
//            List<SongListUser> songListUserList = songListUserDao.selectList(songListUserQueryWrapper);
//            if (!CollectionUtils.isEmpty(songListUserList)){
//                SongListUser songListUser = songListUserList.get(0);
//                Integer songId = songListUser.getSongId();
//                Song song = songDao.selectOne(new QueryWrapper<Song>().eq("id", songId));
//                if (song != null && StringUtils.isNotBlank(song.getCoverUrl())) {
//                    sharedSongListVO.setCoverUrl(song.getCoverUrl());
//                }
//            }
//        }

        Map<String, Object> result = new HashMap<>();
        result.put("sharedSongListTotal", total);
        result.put("sharedSongListList", sharedSongListVOList);
        return result;
    }

    @Override
    public IPage<SharedSongListVO> selectPage(Page<SharedSongListVO> sharedSongListPageParam, SharedSongList sharedSongList) {
        Page<SharedSongListVO> sharedSongListVOPage = sharedSongListDao.selectSharedSongListVOPage(sharedSongListPageParam, null);
        return sharedSongListVOPage;
    }

}
